package com.shhm.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.business.mapper.ProductMapper;
import com.shhm.business.mapper.ProductStatusMapper;
import com.shhm.business.mapper.ProductTypeMapper;
import com.shhm.business.service.ProductService;
import com.shhm.common.dto.request.RequestList;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.dto.response.business.ProductVO;
import com.shhm.common.entity.common.ProductEntity;
import com.shhm.common.entity.common.ProductStatusEntity;
import com.shhm.common.mapper.SysRoleOperatePermsMapper;
import com.shhm.common.utils.JwtUtil;
import com.shhm.common.utils.RequestUtil;
import com.shhm.common.utils.ResponseUtil;
import com.shhm.common.utils.StringUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/9/2 下午3:10
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductStatusMapper productStatusMapper;
    @Resource
    private SysRoleOperatePermsMapper sysRoleOperatePermsMapper;
    @Resource
    private ProductTypeMapper productTypeMapper;

    /**
     * 查询商品数据
     * @param pageNum
     * @param pageSize
     * @param entity
     * @param request
     * @return
     */
    @Override
    public AjaxResultPlus pageSearchList(Integer pageNum, Integer pageSize, ProductVO entity, HttpServletRequest request) {
        PageDTO<ProductVO> userPage = new PageDTO<>(pageNum, pageSize);
        PageDTO<ProductVO> list = productMapper.selectProductsWithStatusByCondition(userPage,entity);
        return AjaxResultPlus.success(list,false);
    }

    /**
     * 卖家上架待审核商品
     * @param entity
     * @param request
     * @return
     */
    @Transactional(rollbackFor = {SQLException.class,Exception.class})
    @Override
    public AjaxResultPlus add(ProductEntity entity, HttpServletRequest request) {
        Long uid = JwtUtil.getUIDFromAuth(request);
        String randomNumber = StringUtil.createRandomNumber("prod" + uid);
        entity.createLocalDateTime();
        entity.setProductNum(randomNumber);
        entity.setSellerId(uid);
//        System.out.println("----> "+ entity);
        ProductStatusEntity pse = new ProductStatusEntity(uid, randomNumber);
        pse.createLocalDateTime();
        if (Objects.isNull(productTypeMapper.selectById(entity.getProductType()))){
            return AjaxResultPlus.success("商品类型不存在",true);
        }
        int r1 = productMapper.insert(entity);
        int r2 = productStatusMapper.insert(pse);
        return AjaxResultPlus.success(true);
    }

    /**
     * 卖家修改商品数据
     * @param entity
     * @param request
     * @return
     */
    @Override
    public AjaxResultPlus update(ProductVO entity, HttpServletRequest request) {
        System.out.println(entity);
//        return null;
        if (Objects.isNull(productTypeMapper.selectById(entity.getProductType()))){
            return AjaxResultPlus.error("商品类型不存在");
        }
        return ResponseUtil.sqlExeIsSuccess(productMapper.customUpdate(entity));
    }

    @Transactional(rollbackFor = {SQLException.class,Exception.class})
    @Override
    public AjaxResultPlus delete(RequestList ids, HttpServletRequest request) {
        List<Object> list = ids.getList();
        productMapper.customDeleteProduct(list,"tb_product");
        productMapper.customDeleteProduct(list,"tb_product_status");
        return AjaxResultPlus.success(true);
    }

    /**
     * 查询已上架的商品数据
     * @param pageNum
     * @param pageSize
     * @param isPage
     * @param request
     * @return
     */
    @Override
    public AjaxResultPlus shelvesList(Integer pageNum, Integer pageSize, Integer isPage, HttpServletRequest request) {
        PageDTO<ProductVO> defPage = new PageDTO<>(pageNum, pageSize);
        PageDTO<ProductVO> list = getProductStatusList(123,defPage);
        String userOperatePerms = RequestUtil.getUserOperatePerms(request, sysRoleOperatePermsMapper);
//        if (userOperatePerms.equals("NOT_PERMISSION")) return AjaxResultPlus.error(403,"您无权查看数据");
        return AjaxResultPlus.custom(200, false)
                .put("data",list)
                .put("operateStr",userOperatePerms);
//        return null;
    }

    /**
     * 审核商品
     * @param entity
     * @param request
     * @return
     */
    @Override
    public AjaxResultPlus auditProduct(ProductVO entity, HttpServletRequest request) {
        Long uid = JwtUtil.getUIDFromAuth(request);
        entity.setOperateId(uid);
        return ResponseUtil.sqlExeIsSuccess(productStatusMapper.auditProduct(entity));
    }

    /**
     * 商品审核列表
     * @param pageNum
     * @param pageSize
     * @param isPage
     * @param request
     * @return
     */
    @Override
    public AjaxResultPlus auditProductList(Integer pageNum, Integer pageSize, Integer isPage, HttpServletRequest request) {
        PageDTO<ProductVO> defPage = new PageDTO<>(pageNum, pageSize);
//        PageDTO<ProductVO> list = getProductStatusList(4,defPage);
        PageDTO<ProductVO> list = productMapper.pageList(defPage);
        String userOperatePerms = RequestUtil.getUserOperatePerms(request, sysRoleOperatePermsMapper);
//        if (userOperatePerms.equals("NOT_PERMISSION")) return AjaxResultPlus.error(403,"您无权查看数据");
        return AjaxResultPlus.custom(200, false)
                .put("data",list)
                .put("operateStr",userOperatePerms);
    }

    @Override
    public AjaxResultPlus sellerProductList(Integer pageNum, Integer pageSize, Integer isPage, HttpServletRequest request) {
        PageDTO<ProductVO> defPage = new PageDTO<>(pageNum, pageSize);
        ProductVO entity = new ProductVO();
        Long uid = JwtUtil.getUIDFromAuth(request);
        entity.setSellerId(uid);
        PageDTO<ProductVO> list = productMapper.selectProductsWithStatusByCondition(defPage, entity);
        return AjaxResultPlus.custom(200, false)
                .put("data",list)
                .put("operateStr","*");
    }

    @Override
    public AjaxResultPlus updateProductEntity(ProductEntity entity, HttpServletRequest request) {
        return ResponseUtil.sqlExeIsSuccess(productMapper.updateById(entity));
    }

    public PageDTO<ProductVO> getProductStatusList(Integer productStatus,PageDTO<ProductVO> page){
        ProductVO entity = new ProductVO();
        entity.setProductStatus(productStatus);
        return productMapper.selectProductsWithStatusByCondition(page,entity);
    }
}
